<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/2 0002
 * Time: 11:18
 */

namespace app\api\controller\v1\cleaner;


use app\api\controller\v1\Cleaner;
use app\currency\service\TimeSlotService;
use library\tools\Data;
use think\Db;

class OrderPre extends Cleaner
{
    public function read($id)
    {

    }

    /**
     * 创建预订单，用于用户扫码直接下单
     * 返回一个二维码地址
     */
    public function save()
    {
        $data = $this->_input([
            'cities_id' => $this->request->post('cities_id'),//地址
            'contact_name' => $this->request->post('contact_name'),
            'contact_phone' => $this->request->post('contact_phone'),
            'service_date' => $this->request->post('service_date'),
            'time_slot' => $this->request->post('time_slot'),
            'house_type' => $this->request->post('house_type'),
            'service_duration' => $this->request->post('service_duration'),//服务时长
        ],[
            'cities_id' => 'require',
            'contact_name' => 'require',
            'contact_phone' => 'require|mobile',
            'service_date' => 'require|date',
//            'time_slot' => 'require|number',
            'house_type' => 'require',
            'service_duration' => 'require|number',
        ]);
        !empty($data['time_slot']) || $data['time_slot'] = '1';
        if($this->cleaner->auth_state != '1')
        {
            $this->error('请先提交实名认证！');
        }
        $cities = Db::name('Cities')->where('id',$data['cities_id'])->find();
        empty($cities) && $this->error('地址信息查询失败！');
        //修改地址数据
        Db::name('Cities')->where('id',$data['cities_id'])->update([
            'house_type' => $data['house_type'],
            'service_duration' => $data['service_duration'],
        ]);
        //获取服务时间
        $time_slot = TimeSlotService::getTimeSlot($data['time_slot']);
        if($time_slot === ''){
            $this->error('请选择正确的服务时间！');
        }
        //插入数据库
        $data['cid'] = $this->cid;
        $order_no = Data::uniqidNumberCode(12);
        $data['order_no'] = $order_no;
        $res = Db::name('CleanOrderPre')->strict(false)->insert($data);
        if($res)
        {
            $this->success('success',['order_no' => $order_no]);
        } else {
            $this->error('创建失败！');
        }

    }
}